#!/bin/bash

#eval $(xdotool getdisplaygeometry --shell)

opt=${@:1:1}

if [ "init" == "$opt" ]; then
    olv init work
elif [ "cp" == "$opt" ]; then
    input=${@:2:1}
    docker cp $input popresearch_frontend_1:/app/user/$input
elif [ "firefox" == "$opt" ]; then
    olv firefox work 
    exit
    #o wfd results 4863
    target=${@:2:1}
    page=${@:3:1}
    input=${@:4:1}
    http='http'
    host='localhost'
    if [ "dev" == "$target" ]; then
	host='http://localhost:8000/'
    elif [ "prod" == "$target" ]; then
	http='https'
    fi

    if [ "results" == "$page" ]; then
	echo 1234
    fi
elif [ "shell" == "$opt" ]; then
    input=${@:2:1}
    if [ -z "$input" ]; then
	echo "You didn't specify a docker container"
    else
	env=$(docker ps | grep $input | sed 's/.* //')
	docker exec -it $env bash
    fi
elif [ "reload" == "$opt" ]; then
    screen -S work -p 0 -X stuff "^C"
    screen -S work -p 0 -X stuff "gulp^M"
elif [ "stop" == "$opt" ]; then
    screen -S work -p 0 -X stuff "^C"
elif [ "kill" == "$opt" ]; then
    screen -S work -p 0 -X stuff "^C"
    screen -S work -p 0 -X stuff "^D"
elif [ "dev" == "$opt" ]; then
    screen -S work -p 0 -X stuff "./dev.sh^M"
    screen -S work -p 0 -X title dev
elif [ "prod" == "$opt" ]; then
    screen -S work -p 0 -X stuff "./prod.sh^M"
    screen -S work -p 0 -X title prod
elif [ "start" == "$opt" ]; then
    func=${@:2:1}

    if [ -z "$func" ]; then
    #docker ps | grep frontend | wc -l
	screen -S work -p 0 -X stuff "gulp^M"
    else
	cmd="./"$func".sh^M"
	screen -S work -p 0 -X stuff $cmd
	screen -S work -p 0 -X title $func
    fi
else
    if [ -z "$opt" ]; then
	opts=$(grep "==.*\$opt" $0 | grep -v grep | sed 's/[^"]*"//;s/".*//')
	echo "Work Options: "$opts
    else
	#short hand
	c=$(echo "$opt" | sed 's/[0-9]//g')
        opts=$(grep "==.*\$opt" $0 | grep -v grep | sed 's/[^"]*"//;s/".*//')
	list=$(echo "$opts" | grep ^$c)
	opts=($(echo $list))
	count=${#opts[@]}

	if [ "2" -gt "$count" ]; then
	    olv work ${opts[0]} ${@:2}
	else
	    index=$(echo $opt | sed 's/[^0-9]//g')
	    if [ -z "$index" ]; then
		echo "Options:"
		echo "$list" | nl -v0
		read -p "olv work $c#: " index
		
		index=$(echo "$index" | sed 's/[^0-9]//g')
		if [ -z "$index" ]; then
		    echo "No option selected."
		    exit
		fi
	    fi

	    olv work ${opts[$index]} ${@:2}
	fi
    fi
fi
